// components/activity/activity-base-edit/index.js
import componentBehavior from '../../../behaviors/component';
import { COMPONENT_OPTIONS } from'../../../constants/common';
import { ACTIVITY_INTRO_TYPE, ACTIVITY_OWNER_VALUE, ACTIVITY_INTRO_TYPE_VALUE } from '../../../constants/activity';
import { obj2Arr, swapArr, deepClone } from '../../../common/utils';
import { checkImageSecurity, tipToast } from '../../../common/env';
// import { uploadImage, uploadVideo } from '../../../common/qiniu';
import { uploadImage, uploadVideo } from '../../../common/cos';
Component({
    behaviors: [componentBehavior],
    options: {
        ...COMPONENT_OPTIONS,
    },
    properties: {
        activityInfo: {
            type: Object,
            value: {},
        },
    },
    observers: {
        'activityInfo': function (val) {
            if (val && val.intro) {
                const { cosConf } = this.data;
                const cloneVal = deepClone(val);
                let introInfo = cloneVal.intro.map(i => {
                    if (i.type !== ACTIVITY_INTRO_TYPE_VALUE.text) {
                        i.resource = i.resource ? i.resource.split(',') : [];
                        i.resource = i.resource.map(r => {
                            const obj = { url: `${cosConf.path}/${r}` };
                            if (i.type === ACTIVITY_INTRO_TYPE_VALUE.video) obj.isVideo = true;
                            return obj;
                        });
                    }
                    return i;
                });
                this.setData({ introInfo });
            }
        },
    },
    data: {
        introTypes: obj2Arr(ACTIVITY_INTRO_TYPE),
        introInfo: [],
    },
    methods: {
        _handleChange(data) {
            const { cosConf, activityInfo } = this.data;
            data.intro && (data.intro = data.intro.map(i => {
                if (i.type !== 'text') {
                    i.resource = i.resource.map(r => r.url.replace(`${cosConf.path}/`, ''));
                    i.resource = i.resource.join(',');
                }
                return i;
            }));
            this.triggerEvent('change', {
                ...activityInfo,
                ...data,
            });
        },
        handleChangeName(e) {
            const name = e.detail;
            this._handleChange({ name });
        },
        handleChangeText(e) {
            const value = e.detail;
            const { index } = e.currentTarget.dataset;
            const { introInfo } = this.data;
            introInfo[index].resource = value;
            this._handleChange({ intro: introInfo });
        },
        async handleUploadResource(e) {
            const { index, type } = e.currentTarget.dataset;
            const { introInfo } = this.data;
            const { file } = e.detail;
            if (type !== 'video') {
                const flag = await checkImageSecurity(file.url);
                if (!flag) {
                    tipToast('请确认图片合法、合规');
                    return; 
                }
            }
            const res = type === 'video' ? await uploadVideo('activity', file.url) : await uploadImage('activity', file.url);
            introInfo[index].resource.push({ url: res.url });
            this._handleChange({ intro: introInfo });
        },
        handleDeleteResource(e) {
            const idx = e.detail.index;
            const { index, type } = e.currentTarget.dataset;
            const { introInfo } = this.data;
            introInfo[index].resource.splice(idx, 1);
            this._handleChange({ intro: introInfo });
        },
        handleUp(e) {
            const { index } = e.currentTarget.dataset;
            const { introInfo } = this.data;
            if (index === 0) return;
            this._handleChange({ intro: swapArr(introInfo, index, index - 1) });
        },
        handleDown(e) {
            const { index } = e.currentTarget.dataset;
            const { introInfo } = this.data;
            if (index === introInfo.length - 1) return;
            this._handleChange({ intro: swapArr(introInfo, index, index + 1) });
        },
        handleRemove(e) {
            const { index } = e.currentTarget.dataset;
            const { introInfo } = this.data;
            introInfo.splice(index, 1);
            this._handleChange({ intro: introInfo });
        },
        handleAdd(e) {
            const { type } = e.currentTarget.dataset;
            const { introInfo = [] } = this.data;
            let resource = type !== 'text' ? [] : '';
            introInfo.push({ type, resource });
            this._handleChange({ intro: introInfo });
        },
    }
})
